package com.liuyi.util.alipay;


import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.liuyi.util.ResourceUtils;
import com.liuyi.util.alipay.OrderInfoUtil;
import com.liuyi.util.model.AlipayRefund;

public class PayUtils {

	/**
	 * 退款接口
	 * @Author: HONGLINCHEN
	 * @Description: 支付宝退款 退款的金额 不能大于交易金额
	 * store_id 可选 代表 商户的门店编号
	 * terminal_id 可选 代表 商户的终端编号
	 * operator_id 可选 代表 商户的操作员编号
	 * refund_reason 可选 代表 退款的原因说明
	 * out_request_no 可选 标识一次退款请求，同一笔交易多次退款需要保证唯一（就是out_request_no在2次退款一笔交易时，要不一样），如需部分退款，则此参数必传
	 * tradeNo 必须 支付宝交易号
	 * outTradeNo 必须 商户订单号
	 * refundAmount 必须 退款金额
	 * refundInstructions 退款说明
	 * @Date: 2017-12-23 15:37
	 */
	 public static boolean alipayRefundRequest(String tradeNo,String outTradeNo,float refundAmount,String refundInstructions) throws AlipayApiException, IllegalAccessException {
		 String APPID = ResourceUtils.getString("config", "alipay.appId");
		 String RSA2_PRIVATE = ResourceUtils.getString("config", "alipay.rsa2.private");
		 String ALIPAY_PUBLIC_KEY = ResourceUtils.getString("config", "alipay.public.key");
		 AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",APPID, RSA2_PRIVATE, "json", "UTF-8",ALIPAY_PUBLIC_KEY,"RSA2");
		 AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		 AlipayRefund alipayRefund= new AlipayRefund();
		 alipayRefund.setOut_trade_no(outTradeNo);//这个是商户的订单号
		 alipayRefund.setTrade_no(tradeNo);//这个是支付宝的订单号
		 alipayRefund.setRefund_amount(refundAmount);//退款金额
		 alipayRefund.setRefund_reason(refundInstructions);//退款说明
		 alipayRefund.setOut_request_no("HZ01RF001");
		 alipayRefund.setOperator_id("OP001");
		 alipayRefund.setStore_id("NJ_S_001");
		 alipayRefund.setTerminal_id("NJ_T_001");//request.setBizContent(JSONUtil.simpleObjectToJsonStr(alipayRefund));
		 request.setBizContent(JSONObject.toJSONString(alipayRefund));//2个都可以，这个参数的顺序 不影响退款
		 AlipayTradeRefundResponse response = alipayClient.sdkExecute(request);
		 if (response.isSuccess()) {
			return true;
		 } else {
			return false;
		 }
	 }
	 
	/**
	 * 
	 * @Title: activityAlipayKey 
	 * @Description: 生成订单信息
	 * @param did
	 * @param money
	 * @return
	 * @return: String
	 */
	public String activityAlipayKey(String did,Float money) {
		String APPID = ResourceUtils.getString("config", "alipay.appId");
		String PID = ResourceUtils.getString("config", "alipay.pid");
		String RSA2_PRIVATE = ResourceUtils.getString("config", "alipay.rsa2.private");
	    boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> params = OrderInfoUtil.buildOrderParamMap(APPID,did,money,rsa2);
        String orderParam = OrderInfoUtil.buildOrderParam(params);
        String privateKey = RSA2_PRIVATE;
        String sign = OrderInfoUtil.getSign(params, privateKey, rsa2);
        final String orderInfo = orderParam + "&" + sign;
		return orderInfo;
    }
}
